import { useReducer } from 'react';

export interface IAuthGranReducerStateType {
  stepsIndex: number;
  leftSearchValue: string;
  leftSelectKeys: string[];
  leftExpandedKeys: string[]; //展开的值
  autoExpandParent: boolean; //左侧树是否自动展开
  leftTreeCheckRows: Record<string, any>[]; //保存左侧树选中值
  rightSearchValue: string; //右侧输入框值

  firstLeftTreeKeys: string[]; //第一步左侧选中的参数
  firstRightListRows: Record<string, any>[]; //第一步左侧选中的参数

  secondMenuOpenKeys: string[]; // 第二步初始化展开的菜单ID
  secondMenuCheckKeys: string[]; //第二步选中的菜单  id
  secondLeftSearValue: string; //第二步输入搜索框 内容左侧
  secondRightSearchValue: string; //右侧搜索框 内容
  secondSaveIdList: any; //第二步的参数保存
  threeLeftSearValue: string; //第三步输入搜索框 内容左侧
  threeMenuCheckKeys: string[]; //第三步选中的菜单  id
  threeMenuOpenKeys: string[]; // 第二步初始化展开的菜单ID
  threeSaveIdList: any; //第三步的参数保存
  threeRightSearchValue: string; //右侧搜索框 内容
  // roleAuthDetailInfo: Record<string, any>  //详情
}

interface IActionType {
  type:
    | 'authGran-stepsIndex'
    | 'authGran-leftSearchValue'
    | 'authGran-leftSelectKeys'
    | 'authGran-leftExpandedKeys'
    | 'authGran-autoExpandParent'
    | 'authGran-leftTreeCheckRows'
    | 'authGran-rightSearchValue'
    | 'authGran-firstLeftTreeKeys'
    | 'authGran-firstRightListRows'
    | 'authGran-secondMenuOpenKeys'
    | 'authGran-secondMenuCheckKeys'
    | 'authGran-secondLeftSearValue'
    | 'authGran-secondRightSearchValue'
    | 'authGran-secondSaveIdList'
    | 'authGran-threeLeftSearValue'
    | 'authGran-threeMenuCheckKeys'
    | 'authGran-threeMenuOpenKeys'
    | 'authGran-threeSaveIdList'
    | 'authGran-threeRightSearchValue'
    // | 'authGran-roleAuthDetailInfo'
    | 'remove-all-state';
  payload: any;
}

export const useAuthGranReducer = () => {
  const initState: IAuthGranReducerStateType = {
    stepsIndex: 0,
    leftSearchValue: '',
    leftSelectKeys: [],
    leftExpandedKeys: [],
    autoExpandParent: false,
    leftTreeCheckRows: [],
    rightSearchValue: '',
    firstLeftTreeKeys: [],
    firstRightListRows: [],
    secondMenuOpenKeys: [],
    secondMenuCheckKeys: [],
    secondLeftSearValue: '',
    secondRightSearchValue: '',
    secondSaveIdList: null,
    threeLeftSearValue: '',
    threeMenuCheckKeys: [],
    threeMenuOpenKeys: [],
    threeSaveIdList: null,
    threeRightSearchValue: '',
    // roleAuthDetailInfo: {}
  };

  const reducer = (state: IAuthGranReducerStateType, actions: IActionType) => {
    switch (actions?.type) {
      // case 'authGran-roleAuthDetailInfo':
      //   return {
      //     ...state,
      //     roleAuthDetailInfo: actions?.payload,
      //   };

      case 'authGran-threeRightSearchValue':
        return {
          ...state,
          threeRightSearchValue: actions?.payload,
        };

      case 'authGran-threeSaveIdList':
        return {
          ...state,
          threeSaveIdList: actions?.payload,
        };

      case 'authGran-threeMenuOpenKeys':
        return {
          ...state,
          threeMenuOpenKeys: actions?.payload,
        };

      case 'authGran-threeMenuCheckKeys':
        return {
          ...state,
          threeMenuCheckKeys: actions?.payload,
        };

      case 'authGran-threeLeftSearValue':
        return {
          ...state,
          threeLeftSearValue: actions?.payload,
        };

      case 'authGran-secondSaveIdList':
        return {
          ...state,
          secondSaveIdList: actions?.payload,
        };

      case 'authGran-secondRightSearchValue':
        return {
          ...state,
          secondRightSearchValue: actions?.payload,
        };

      case 'authGran-secondLeftSearValue':
        return {
          ...state,
          secondLeftSearValue: actions?.payload,
        };

      case 'authGran-secondMenuCheckKeys':
        return {
          ...state,
          secondMenuCheckKeys: actions?.payload,
        };

      case 'authGran-secondMenuOpenKeys':
        return {
          ...state,
          secondMenuOpenKeys: actions?.payload,
        };

      case 'authGran-firstRightListRows':
        return {
          ...state,
          firstRightListRows: actions?.payload,
        };

      case 'authGran-firstLeftTreeKeys':
        return {
          ...state,
          firstLeftTreeKeys: actions?.payload,
        };

      case 'authGran-stepsIndex':
        return {
          ...state,
          stepsIndex: actions?.payload,
        };

      case 'authGran-leftSearchValue':
        return {
          ...state,
          leftSearchValue: actions?.payload,
        };

      case 'authGran-leftSelectKeys':
        return {
          ...state,
          leftSelectKeys: actions?.payload,
        };

      case 'authGran-leftExpandedKeys':
        return {
          ...state,
          leftExpandedKeys: actions?.payload,
        };

      case 'authGran-autoExpandParent':
        return {
          ...state,
          autoExpandParent: actions?.payload,
        };

      case 'authGran-leftTreeCheckRows':
        return {
          ...state,
          leftTreeCheckRows: actions?.payload,
        };
      case 'authGran-rightSearchValue':
        return {
          ...state,
          rightSearchValue: actions?.payload,
        };

      case 'remove-all-state': {
        return {
          ...state,
          ...initState,
        };
      }
      default:
        return {
          ...state,
        };
    }
  };

  const [state, authGranDispatch] = useReducer(reducer, initState);
  return {
    ...state,
    authGranDispatch,
  };
};
